#pragma once


//-----------------------------------------------------------------------------
//															game libraries names
//-----------------------------------------------------------------------------
static const char* SYSTEM_LIB_NAME			= "System.dll";
static const char* DX9_RENDER_LIB_NAME	= "DX9Render.dll";
static const char* INPUT_LIB_NAME				= "Input.dll";

//-----------------------------------------------------------------------------
//															render api libraries names
//-----------------------------------------------------------------------------
static const char* DX9_LIB_NAME					= "DX9Layer.dll";

//-----------------------------------------------------------------------------
//													module registration macro
//-----------------------------------------------------------------------------
#define REGISTER_GAME_MODULE(Interface, Implementation) \
	extern "C" DllExport Interface* RegisterGameModule() \
	{	\
		static Interface* pModule = new Implementation; \
		return pModule; \
	}

//-----------------------------------------------------------------------------
//														module loader function
//-----------------------------------------------------------------------------
template <class Interface>
Interface* LoadGameModule(const char* libName)
{
	Interface* pModule = nullptr;
	XLIBRARY hLib = XApi::LoadLibrary(libName);
	if (hLib)
	{
		typedef Interface* (*PFNMODULEREGISTRATOR)();
		PFNMODULEREGISTRATOR RegisterFunc = reinterpret_cast<PFNMODULEREGISTRATOR>(XApi::GetProcAddress(hLib, "RegisterGameModule"));
		if (RegisterFunc)
		{
			pModule = RegisterFunc();
			if (pModule && !pModule->Init())
			{
				//	module initialization failed - cleanup memory
				delete pModule;
			}
		}
	}
	return pModule;
}

//-----------------------------------------------------------------------------
//												render api module registration macro
//-----------------------------------------------------------------------------
#define REGISTER_RENDER_API_MODULE(Implementation) \
	extern "C" DllExport IRenderAPI* RegisterRenderAPIModule() \
{ \
	static IRenderAPI* pModule = new Implementation; \
	return pModule; \
}

//-----------------------------------------------------------------------------
//												render api module loader function
//-----------------------------------------------------------------------------
inline IRenderAPI* LoadRenderAPIModule(const char* libName)
{
	IRenderAPI* pModule = nullptr;
	XLIBRARY hLib = XApi::LoadLibrary(libName);
	if (hLib)
	{
		typedef IRenderAPI* (*PFNMODULEREGISTRATOR)();
		PFNMODULEREGISTRATOR RegisterFunc = reinterpret_cast<PFNMODULEREGISTRATOR>(XApi::GetProcAddress(hLib, "RegisterRenderAPIModule"));
		if (RegisterFunc)
		{
			pModule = RegisterFunc();
			if (pModule && !pModule->Init())
			{
				//	module initialization failed - cleanup memory
				delete pModule;
			}
		}
	}
	return pModule;
}